package com.kidback.bbloc259.app;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.mmkv.MMKV;

import java.lang.ref.WeakReference;

/**
 * Created by meikai on 2020/06/02.
 */
public class KidApplication extends Application {

    private static KidApplication instance;

    public static WeakReference<Activity> currentActivityRef;

    //第三方app和微信通信的openApi接口
    public IWXAPI api;

    public static KidApplication getInstance() {
        return instance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        instance = this;

    }

    public static Activity getCurrentActivity() {
        return currentActivityRef.get();
    }

    public static AppCompatActivity getCompactActivity() {
        return (AppCompatActivity) currentActivityRef.get();
    }

    @Override
    public void onCreate() {
        super.onCreate();

        MMKV.initialize(KidApplication.getInstance());

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
                currentActivityRef = new WeakReference<>(activity);
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                currentActivityRef = new WeakReference<>(activity);
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });
    }

    public void init(){

        KidApplicationHelper.initApplication(this);


        initWXLogin();
    }

    private void initWXLogin() {
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, KidGlobal.WX_APP_ID, true);
        // 将应用的appId注册到微信
        api.registerApp(KidGlobal.WX_APP_ID);

        //建议动态监听微信启动广播进行注册到微信
        IntentFilter filter = new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP);
        registerReceiver(broadcastReceiver, filter, RECEIVER_EXPORTED);
    }

    private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 将该app注册到微信
            api.registerApp(KidGlobal.WX_APP_ID);

        }
    };


}
